package com.study.lock;

import java.util.concurrent.locks.ReentrantLock;

// 第一步 创建资源类
class LTicket {

    // 票数
    private int number = 30;

    // 创建可重入锁
    private final ReentrantLock lock = new ReentrantLock(true);

    // 卖票
    public void sale() {
        // 上锁
        lock.lock();
        try {
            // 判断是否有票
            if (number > 0) {
                System.out.println(Thread.currentThread().getName()+"：卖出："+(number--)+" 张票，还剩余："+number+" 张");
            }
        } finally {
            lock.unlock();
        }
    }

}


/**
 * @author wanghao
 * @version v1.0
 * @description:
 * @date 2021/12/24 16:33
 * @change [修改时间] [修改者]@[版本号] [修改内容说明]
 */
public class LSaleTicket {

    public static void main(String[] args) {
        LTicket ticket = new LTicket();

        new Thread(()->{
            for (int i = 0; i < 30; i++) {
                ticket.sale();
            }
        },"AA").start();

        new Thread(()->{
            for (int i = 0; i < 30; i++) {
                ticket.sale();
            }
        },"BB").start();

        new Thread(()->{
            for (int i = 0; i < 30; i++) {
                ticket.sale();
            }
        },"CC").start();
    }

}
